package com.example.common.slice;

import com.example.common.ResourceTable;
import com.example.common.fraction.TextListFraction;
import com.example.common.fraction.VideoFraction;
import com.example.commonlib.base.ToolbarAbility;
import com.example.commonlib.base.model.SuperModel;
import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;

public class MainAbilitySlice extends ToolbarAbility {
    private  String[] str={"课程","视频"};
    private StackLayout stackLayout;
    private ComponentContainer myLayout;
    private final int leftIconVal=0; //左侧无图标情况
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        SuperModel.initialize(getContext());
        // 由于无法直接 通过 RsId setUI, 所以 通过下面获取componentContainer，再set
        LayoutScatter getLayoutInflater = LayoutScatter.getInstance(getContext());
        myLayout = (ComponentContainer)getLayoutInflater.parse(ResourceTable.Layout_ability_main, null, false);

        setStatusPageEnable(false);
        // set UI
        setUIContent(myLayout);
        // 设置显隐藏 返回
        setToolbarHomeBack(true);

        // set toolbar tilte
        setTitle(ResourceTable.String_title);

        // 设置左侧icon
        setLeftIcon(leftIconVal);

        // set right button text
        setRightText(ResourceTable.String_btnRight);

        initview();
    }

    // toolbar 继承返回点击事件
    @Override
    public void backEvent(){
        System.out.println("parent back");
    }

    // toolbar 继承右侧点击事件
    @Override
    public void rightEvent(){
        System.out.println("parent right");
        present(new CatalogAbilitySlice(), new Intent());
    }

    public void initview(){
        stackLayout = (StackLayout) findComponentById(ResourceTable.Id_mainstack);
        TabList tabList = (TabList) findComponentById(ResourceTable.Id_tab_list);
        tabList.setFixedMode(true);

        for (int i = 0; i < str.length; i++) {
            TabList.Tab tab = tabList.new Tab(getContext());
            tab.setText(str[i]);
            tabList.addTab(tab);
        }
        tabList.selectTabAt(0);
        addHomeFraction();

        tabList.addTabSelectedListener(new TabList.TabSelectedListener() {
            @Override
            public void onSelected(TabList.Tab tab) {
                // 当某个Tab从未选中状态变为选中状态时的回调
                layoutShow(tab.getPosition());

            }
            @Override
            public void onUnselected(TabList.Tab tab) {
                // 当某个Tab从选中状态变为未选中状态时的回调

            }
            @Override
            public void onReselected(TabList.Tab tab) {
                // 当某个Tab已处于选中状态，再次被点击时的状态回调

            }
        });

    }
    public void layoutShow(int position){
        switch (position) {
            case 0:
//               加载课程页面
                ((FractionAbility)getAbility()).getFractionManager()
                        .startFractionScheduler()
                        .replace(ResourceTable.Id_mainstack, new VideoFraction())
                        .submit();
                break;
            case 1:
//               加载视频页面
                ((FractionAbility)getAbility()).getFractionManager()
                        .startFractionScheduler()
                        .replace(ResourceTable.Id_mainstack, new TextListFraction())
                        .submit();
                break;

        }
    }
    private void  addHomeFraction(){
        ((FractionAbility)getAbility()).getFractionManager()
                .startFractionScheduler()
                .add(ResourceTable.Id_mainstack, new VideoFraction())
                .submit();
    }
    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

}
